<?php

namespace App\Models\Traits\Fields;

trait UserFields
{
    //表单所有设置
    protected $formSet = [];

    //新建时忽略,自动生成不可由用户更改的字段
    protected $ignoreCreate = ['id', 'created_at', 'update_at', 'mslug', 'remember_token'];

    //修改时忽略
    protected $ignoreUpdate = ['created_at', 'update_at', 'mslug', 'remember_token'];

    protected $gridFields = ['id', 'avatar', 'name', 'username', 'email', 'created_at', 'updated_at'];

    //修改时与新建时设置不一样的，此设置在修改时将替换新建时的设置，如有闭包函数等可直接在addUpdateReplace方法中修改
    protected $updateReplace = [
        'name'  => [
            'display' => '登录名',
        ],
    ];
    // 枚举设置
    protected $enums = [];


    //$formSet的增强设置方式，将补充或替换$formSet，以防匿名函数等出错
    public function addFormSet(): array
    {
        return [
            'id'    => [
                'display' => 'ID'
            ],
            'name'  => [
                'text' => '登录名',
                'creationRules' => ['required', "unique:users"],
                'updateRules' => ['required', "unique:users,name,{{id}}"],
                'required'    => null
            ],
            'username' => [
                'text' => '呢称',
                'creationRules' => ['required', "unique:users"],
                'updateRules' => ['required', "unique:users,username,{{id}}"],
                'required'    => null
            ],
            'email' => [
                'email' => '邮箱',
                'creationRules' => ['required', "unique:users"],
                'updateRules' => ['required', "unique:users,email,{{id}}"],
                'required'    => null
            ],
            'phone' => [
                'mobile' => '手机',
                'creationRules' => ['required', "unique:users"],
                'updateRules' => ['required', "unique:users,phone,{{id}}"],
                'required'    => null,
                'options' => ['mask' => '999 9999 9999']
            ],
            'avatar' => [
                'image' => '头像',
            ],
            'password' => [
                'password' => '密码',
                'rules'     => 'required|confirmed',
            ],
            'password_confirmation' => [
                'password' => '重复密码',
                'rules'     => 'required',
                'default'   => function ($form) {
                    return $form->model()->password;
                }
            ],
            '0'  => [
                'ignore' => ['password_confirmation'],
            ],
            '1'    => [
                'saving' => function ($form) {
                    if ($form->password && $form->model()->password != $form->password) {
                        $form->password = bcrypt($form->password);
                    }
                }
            ],
            'created_at'    => [
                'display' => '注册日期'
            ],
            'updated_at'    => [
                'display' => '活动日期'
            ],
        ];
    }


    //以防匿名函数等出错时在此设$formUpdateReplace
    public function addUpdateReplace(): array
    {
        return [];
    }

    //未设$formSet时自动生成的$formSet
    public function initFormSet()
    {
        $fields = $this->getSaveFields();
        $formSet = [];
        foreach ($fields as $k => $val) {
            if ($val == 'id') {
                $formSet[$val] = [
                    'display' => 'ID'
                ];
            } else {
                $formSet[$val] = [
                    'text' => ucfirst($val)
                ];
            }
        }
        return $formSet;
    }

    //所在设置字段
    public function getFormSet()
    {
        $set = array_merge($this->formSet, $this->addFormSet());
        if (!empty($set)) {
            return $set;
        } else {
            return $this->initFormSet();
        }
    }

    //新建时返回
    public function getFormCreate()
    {
        $fields = array_diff_key($this->getFormSet(), array_flip($this->ignoreCreate));
        return $fields;
    }
    //修改时返回
    public function getFormUpdate()
    {
        $fields = array_merge($this->getFormSet(), $this->updateReplace, $this->addUpdateReplace());
        $fields = array_diff_key($fields, array_flip($this->ignoreUpdate));
        return $fields;
    }


    public function getComments()
    {
        $fields = $this->getFormSet();
        $comments = [];
        foreach ($fields as $key => $value) {
            if (is_numeric($key) || !is_array($value) || empty($value)) continue;
            $comments[$key] = ['type' => key($value), 'title' => array_shift($value)];
        }
        return $comments;
    }

    //取grid时字段
    public function getGridFields()
    {
        $comments = $this->getComments();
        $fields = $this->gridFields ?? $this->getTableFields();
        $grids = [];
        foreach ($fields as $key => $value) {
            $grids[$value] = empty($comments[$value]) ? ['type' => 'text', 'title' => ucfirst($value)] : $comments[$value];
        }
        return $grids;
    }

    public function getEnums()
    {
        return $this->enums ?? [];
    }
}
